package com.nineclock.system.controller;

import com.nineclock.api.system.SysUserControllerApi;
import com.nineclock.common.entity.Result;
import com.nineclock.common.utils.BeanHelper;
import com.nineclock.system.dto.SysRegisterDTO;
import com.nineclock.system.dto.SysUserDTO;
import com.nineclock.system.pojo.SysUser;
import com.nineclock.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author zhangyan
 * @date 2021/3/31 23:29
 */

@RestController
@RequestMapping("/user")
public class SysUserController implements SysUserControllerApi {

    @Autowired
    private SysUserService sysUserService;

    @Override
    @GetMapping("/query")
    public Result<SysUserDTO> query(@RequestParam("username") String username){

        SysUser sysUser = sysUserService.queryUser(username);

        //BeanHelper.copyProperties() 将sysUser转化为SysUserDTO
        SysUserDTO sysUserDTO = BeanHelper.copyProperties(sysUser, SysUserDTO.class);

        return Result.success(sysUserDTO);
    }

    /**
     * 用户注册
     * @param sysRegisterDTO
     * @return
     */
    @Override
    @PostMapping("/register")
    public Result<Long> register(SysRegisterDTO sysRegisterDTO) {
        Long userId = sysUserService.regiester(sysRegisterDTO);
        return Result.success(userId);
    }
}